#python lenguage import
import re  ## using regularexpression
import copy

# django core import
from django.db import models
from django.db.models.fields import Field

class DBModel(models.Model):

   def _prepare(cls):
      tmp=cls
      while hasattr(tmp,'_parent_class'):
         tmp = tmp._parent_class
         # scan the elem into class dict
         c = None
         for elem in tmp.__dict__:
            # define matching for non core element into the attribute
            p = re.compile('__\w+__')
            # working on field type element
            if not callable(tmp.__dict__[elem]):
               if isinstance(tmp.__dict__[elem], Field):
                  cls.add_to_class(elem,tmp.__dict__[elem])
               elif isinstance(tmp.__dict__[elem], models.Manager):
                  c = copy.deepcopy(tmp.__dict__[elem])
                  c.className = cls
                  cls.add_to_class(elem,None)
                  setattr(cls,elem, c)
               elif not p.match(elem) and not elem == "objects":
                  cls.add_to_class(elem,tmp.__dict__[elem])
            else:
               pass
            if hasattr(c,'_costructor'):  #triky point
               c._costructor()
      try:
         (super(DBModel,cls))._prepare()
      except:
         pass

   _prepare = classmethod(_prepare)
   
   def __init__(self, *args, **kwargs):
      super(DBModel, self).__init__(*args, **kwargs)
